// 添加请求拦截器
import axios from "axios";
import {useTokenStore} from "../store/tokenStore.ts";

const useAxiosInstance = axios.create({
    baseURL: "http://localhost:8080/api/v1.0",
    headers: {
        "Content-type": "application/json"
    },
    withCredentials:true
});

useAxiosInstance.interceptors.request.use(function (config) {
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
useAxiosInstance.interceptors.response.use(function (response) {

    return response;
}, function (error) {
    // 超出 2xx 范围的状态码都会触发该函数。
    // 对响应错误做点什么
    if (error.response.status === 401 ){
        useTokenStore().isAuthenticated = false
        return
    }
    return Promise.reject(error);
});

export default useAxiosInstance